<?php
/**
 * 《零件检测》实现类
 *
 * @author 孙晓晔
 * @version $Id: Quality.class.php 945 2010-02-05 07:27:36Z sunxy $
 */
require_once(dirname(__FILE__) . '/MoldUtil.class.php');

class Testing extends MoldUtil {

	var $info_dir = 'quality';

	public $file = '5';

	public $tbl = Constant::tbl_mold_testing;

	public $status = array(
		'-' => '请选择',

		'1' => '异常',
		'9' => '合格',
	);

	public $groupby = array(
		'-' => '请选择',
		'mold' => '产品',
		'applicant' => '负责人',
		'checker' => '检测员',
		'status' => '状态',
		'1' => '日期',
		'2' => '月份',
	);

	public $orderby = array(
		'-' => '请选择',
		'mold' => '产品',		
		'emp' => '负责人',
		'checker' => '检测员',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	function tab($controller, $request) {		
		$data = array(			
			'零件检测' => array('cc'=>'mold.php?do=Testing.find', 'js'=>''),
			'异常报告' => array('cc'=>'mold.php?do=Exceptions.find', 'js'=>''),
			// '计划质检' => array('cc'=>'mold.php?do=Quality.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-3 day'));

		$controller->display($request, 'mold/testing/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		
		$piece_s = $request['piece_s'];
		$applicant_s = $this->_input($request['applicant_s']);
		$checker_s = $this->_input($request['checker_s']);
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$where = " WHERE 1=1";

		if (!empty($piece_s) && $piece_s <> '-') {
			$where .= " AND piece = '" . $piece_s . "'";
		}

		if (!empty($applicant_s)) {
			$where .= " AND applicant = '" . $applicant_s . "'";
		}
		
		if (!empty($checker_s)) {
			$where .= " AND checker = '" . $checker_s . "'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND date >= '" . $date1_s . "'";
		}
		
		if (!empty($date2_s)) {
			$where .= " AND date < '" . $date2_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
			
		$controller->display($request, 'mold/testing/list');

		return true;
	}

	/**
	 * 添加质检报告
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'mold/testing/add');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$mold = $this->_input($request['mold']);
		$piece = addslashes($request['piece']);
		$work = addslashes($request['work']);

		$applicant = $this->_input($request['applicant']);

		$checker = $this->_input($request['checker']);
		$date = $request['date'];
		$status = $request['status'];
		
		$amount = $request['amount'];
		$material = addslashes($request['material']);

		$content = addslashes($request['content']);
		$exception = addslashes($request['exception']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(mold, piece, work, applicant, amount, material, content, exception, date, checker, emp";
		$fmt .= ", status, createtime)  VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now())";
		$sql = sprintf($fmt, $this->tbl, $mold, $piece, $work, $applicant, $amount, $material, $content, $exception, $date, $checker, $_SESSION['id'], $status);
		// debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mold/testing/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$mold = $this->_input($request['mold']);
		$piece = addslashes($request['piece']);
		$work = addslashes($request['work']);

		$applicant = $this->_input($request['applicant']);

		$checker = $this->_input($request['checker']);
		$date = $request['date'];
		$status = $request['status'];
		
		$amount = $request['amount'];
		$material = addslashes($request['material']);

		$content = addslashes($request['content']);
		$exception = addslashes($request['exception']);
		$solution = addslashes($request['solution']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET mold = '%s', piece = '%s', work = '%s', applicant = '%s', checker = '%s', date = '%s', amount = '%s', ";
		$fmt .= "material = '%s', exception = '%s', solution = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $mold, $piece, $work, $applicant, $checker, $date, $amount, $material, $exception, $solution, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mold/testing/copy');
		return true;
	}
	
	function solution($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mold/testing/solution');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function solutionPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$solution = addslashes($request['solution']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET solution = '%s', content = '%s', status = '9', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $solution, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);		

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mold/testing/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['mold_t'] = Name::mold($row['mold']);

		$row['applicant_t'] = Name::emp($row['applicant']);
		$row['checker_t'] = Name::emp($row['checker']);

		$row = $this->_view_t($row);
				
		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$fmt = "delete from %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function report($controller, $request) {
		$this->pretreat($controller);

		$mold_s = $request['mold_s'];
		$piece_s = $request['piece_s'];
		$technics_s = $request['technics_s'];

		$db = newdb($this->dsn_r);
		$sql = "SELECT * FROM " . $this->tbl;
		$where = " WHERE 1=1";

		if (!empty($mold_s)) {
			$where .= " AND mold = '" . $mold_s . "'";
		}

		if (!empty($piece_s) && $piece_s <> '-') {
			$where .= " AND piece = '" . $piece_s . "'";
		}

		if (!empty($technics_s) && $technics_s <> '-') {
			$where .= " AND technics = '" . $technics_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		$orderby = $this->orderby($request['r1']);
		$sql .= $where . $orderby;
		// debug($sql);
		$result = $db->query($sql);

		while ($row = $db->fetch_array($result)) {
			$row['mold_t'] = Name::mold($row['mold']);

			$produce = $this->id(Constant::tbl_mold_produce, $row['produce']);

			$row['piece'] = $this->piece[$produce['piece']];
			$row['technics'] = $this->technics[$produce['technics']];

			$row['file'] = Download::url($this->info_dir, $row['createtime'], $row['produce'], $row['filename']);
			$row['filesize_t'] = printsize($row['filesize']);

			$row['emp_t'] = Name::emp($row['emp']);

			$row['status_t'] = $this->status[$row['status']];

			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'mold/testing/report');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'mold/testing/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'mold');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'mold') {
			$row['a'] = sprintf("%s [%s]", Name::mold($row['a']), $row['a']);
		} elseif ($px == 'applicant' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	/**
	 * 定时运行
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tj_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$to = 'list.c@tecmold.com';

		$subject = '产品质检异常情况';

		$this->result_s = 6;
		$this->groupby_s = 'applicant';
		$body = $this->_tj_a($controller, $request);

		$this->mail_html($from, $to, $subject, $body);

		logdate($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function _tj_a($controller, $request) {
			
		$this->result_s = 6;
		$this->groupby_s = 'applicant';
		$s = $this->groupby($controller, $this->groupby_s);
		$df = "DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')";

		$fmt = "SELECT %s as a, COUNT(%s) as c FROM %s a WHERE status = '1' AND createtime > %s GROUP BY %s ORDER BY c DESC";
		$sql = sprintf($fmt, $s, $s, $this->tbl, $df, $s);
		echo $sql . "\n";

		$this->_group_tj($controller, $sql);

		return $controller->sunxy($request, 'tj_a');
	}
	
	function view_mold($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_list_mold($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'mold/testing/_list_c');
		return true;
	}
}
?>